package com.ltedu.course.controller;

import com.ltedu.common.contants.Constants;
import com.ltedu.common.validate.Create;
import com.ltedu.common.vo.Result;
import com.ltedu.course.domain.Chapter;
import com.ltedu.course.domain.dto.ChapterDTO;
import com.ltedu.course.domain.vo.ChapterVO;
import com.ltedu.course.service.ChapterService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 课程章节表 前端控制器
 * </p>
 *
 * @author Jerry
 * @since 2020-12-10
 */
@RestController
@RequestMapping("/course/chapter")
public class ChapterController {

    @Autowired
    private ChapterService chapterService;

    /**
     * 添加章节
     *
     * @param chapterVO
     * @return {@link Result}
     */
    @PostMapping("/add")
    public Result add(@Validated(Create.class) @RequestBody ChapterVO chapterVO) {
        Chapter chapter = new Chapter();
        BeanUtils.copyProperties(chapterVO, chapter);
        chapter.setGmtCreate(new Date());
        chapter.setGmtModified(new Date());
        chapter.setState(Constants.NORMAL);
        return Result.ok(chapterService.save(chapter));
    }

    /**
     * 根据课程id获取章节列表
     *
     * @param courseId 进程id
     * @return {@link Result}
     */
    @GetMapping("/getChapterList")
    public Result getChapterList(@RequestParam Long courseId) {
        List<ChapterDTO> chapterList = chapterService.getChapterList(courseId);
        return Result.ok(chapterList);
    }
}

